package com.ctyon.watchmanager.ui.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.ctyon.common.event.HfcxMessageEvent;
import com.ctyon.common.util.DataUtil;
import com.ctyon.common.util.SPUtils;
import com.ctyon.common.util.TSnackbarUtil;
import com.ctyon.netlibrary.net.revert.BaseResponseEntity;
import com.ctyon.netlibrary.use.BaseObserver;
import com.ctyon.watchmanager.BasicApp;
import com.ctyon.watchmanager.R;
import com.ctyon.watchmanager.databinding.FragmentMoneyBinding;
import com.ctyon.watchmanager.model.net.service.NetService;
import com.ctyon.watchmanager.present.MoneyPresenter;
import com.ctyon.watchmanager.ui.BaseFragment;
import com.orhanobut.logger.Logger;
import com.yuyh.library.imgsel.utils.LogUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

import static com.ctyon.common.util.SPUtils.WATCH_LL;
import static com.ctyon.common.util.SPUtils.WATCH_YE;

/**
 * CreateDate：19-6-25 on 上午9:14
 * Describe:
 * Coder: lee
 */
public class MoneyFragment extends BaseFragment {

    public static final String TAG = "MoneyFragment";

    private FragmentMoneyBinding mBinding;
    private int cxFlag = 0;

    public static MoneyFragment newInstance() {
        final MoneyFragment fragment = new MoneyFragment();
        final Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        EventBus.getDefault().register(this);
        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_money, container, false);
        mBinding.setMoneyPresenter(moneyPresenter);

        return mBinding.getRoot();

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        setData();

    }

    private final MoneyPresenter moneyPresenter = new MoneyPresenter() {
        @Override
        public void onCheckYeClick() {
            final String method = "checkMoney";
            if (NetService.getInstance().isDisable(method)){
                TSnackbarUtil.TSBError(getActivity(), "请稍候...");
                return;
            }
            NetService.getInstance()
                    .checkMoney(BasicApp.getInstance().getManager().getManagerId(), BasicApp.getInstance().getWatch().getMeid())
                    .timeout(10, TimeUnit.SECONDS)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new BaseObserver<BaseResponseEntity>() {
                        @Override
                        public void onSuccess(BaseResponseEntity responseEntity) {
                            Logger.d("onSuccess : " + responseEntity.toString());
                            TSnackbarUtil.TSBSuccess(getActivity(), "操作成功");
                            cxFlag = 1;
                        }

                        @Override
                        public String onOver() {
                            return method;
                        }
                    });
        }

        @Override
        public void onCheckLlClick() {
            final String method = "checkData";
            if (NetService.getInstance().isDisable(method)){
                TSnackbarUtil.TSBError(getActivity(), "请稍候...");
                return;
            }
            NetService.getInstance()
                    .checkData(BasicApp.getInstance().getManager().getManagerId(), BasicApp.getInstance().getWatch().getMeid())
                    .timeout(10, TimeUnit.SECONDS)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new BaseObserver<BaseResponseEntity>() {
                        @Override
                        public void onSuccess(BaseResponseEntity responseEntity) {
                            Logger.d("onSuccess : " + responseEntity.toString());
                            cxFlag =2;
                            TSnackbarUtil.TSBSuccess(getActivity(), "操作成功");
                        }

                        @Override
                        public String onOver() {
                            return method;
                        }
                    });
        }
    };

    @Subscribe(threadMode = ThreadMode.MAIN_ORDERED)
    public void getHfcxData(HfcxMessageEvent hfcxMessageEvent){
        Log.e("androidLog_money",hfcxMessageEvent.getContent());
        if(cxFlag==1){
            SPUtils.getInstance().put(WATCH_YE,hfcxMessageEvent.getContent());
        }else if(cxFlag==2){
            SPUtils.getInstance().put(WATCH_LL,hfcxMessageEvent.getContent());
        }
        setData();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    private void setData(){
        String time = DataUtil.getDateToString(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss");
        String ye = SPUtils.getInstance().getString(WATCH_YE);
        String ll = SPUtils.getInstance().getString(WATCH_LL);
        if (!DataUtil.isEmptyOr(ye, ll))
        {
            String sy = getString(R.string.money_ye);
            String sl = getString(R.string.money_ll);
            mBinding.setMoney(String.format(sy, ye, time));
            mBinding.setData(String.format(sl, ll, time));
        }
    }

}
